/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow;

import me.yokeyword.eventbusactivityscope.EventBusAbilityScope;
import me.yokeyword.fragmentation.ISupportFraction;
import me.yokeyword.fragmentation.SupportFraction;
import me.yokeyword.fragmentation.debug.DebugStackDelegate;
import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.base.MySupportAbility;
import me.yokeyword.sample.demo_flow.base.MySupportFraction;
import me.yokeyword.sample.demo_flow.model.UserAccount;
import me.yokeyword.sample.demo_flow.ui.fragment.home.HomeFraction;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.PacMap;
import org.greenrobot.eventbus.Subscribe;

public class MainAbility extends MySupportAbility {
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);

        MySupportFraction fraction = findFraction(HomeFraction.class);
        if (fraction == null) {
            loadRootFraction(ResourceTable.Id_fl_container, HomeFraction.newInstance());
        }
        EventBusAbilityScope.getDefault(this).register(this);
		(new DebugStackDelegate(this)).onPostCreate(this, ResourceTable.Id_fl_container2);
    }

    @Subscribe
    public void onAccountNameChange(UserAccount account) {
        MySupportFraction fraction = HomeFraction.newInstance();
        PacMap newBundle = new PacMap();
        newBundle.putString("UserName", account.accountName);
        fraction.putNewBundle(newBundle);
        start(fraction, SupportFraction.LAUNCHMODE_SINGLETOP);

        ToastDialog toastDialog = new ToastDialog(this);
        toastDialog.setAutoClosable(false);
        toastDialog.setContentText("Sign in successful, SliderComponent's name has changed");
        toastDialog.show();
    }

    @Override
    public void onBackPressedSupport() {
        if (getSupportDelegate().getSupportFractionManagerEx().getBackStackEntryCount() > 1) {
            pop();
        } else {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                terminateAbility();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                getUITaskDispatcher().asyncDispatch(() -> {
                    ToastDialog toastDialog = new ToastDialog(this);
                    toastDialog.setAutoClosable(false);
                    toastDialog.setContentText("Press again to exit");
                    toastDialog.show();
                });
            }
        }
    }

    @Override
    protected void onStop() {
        EventBusAbilityScope.getDefault(this).unregister(this);
        super.onStop();
    }
}

